package com.sambo.inspection.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;
import com.lzy.okgo.model.HttpParams;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.ActivityCache;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.bean.ChangePasswordRequestBean;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.SpUtil;

public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener {

    ImageView ivBack;
    EditText etOldPassword;
    EditText etNewPassword;
    EditText etMakenewPassword;
    TextView tvFinish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword);
        ivBack = findViewById(R.id.iv_back);
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etMakenewPassword = findViewById(R.id.et_makenew_password);
        tvFinish = findViewById(R.id.tv_finish);

        ivBack.setOnClickListener(this);
        etOldPassword.setOnClickListener(this);
        etNewPassword.setOnClickListener(this);
        etMakenewPassword.setOnClickListener(this);
        tvFinish.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_finish:
                resetPassword();
                break;
        }
    }

    private void resetPassword() {

        if (!TextUtils.isEmpty(etNewPassword.getText().toString())
                && TextUtils.equals(etNewPassword.getText().toString(), etMakenewPassword.getText().toString())) {

            ChangePasswordRequestBean requestBean = new ChangePasswordRequestBean();
            requestBean.setOldPassword(etOldPassword.getText().toString());
            requestBean.setPassword(etNewPassword.getText().toString());
            requestBean.setRepassword(etMakenewPassword.getText().toString());
            requestBean.setUsername(GlobleBean.getInstance().getUserName());

            HttpClient.post(this, Urls.API_SERVER_HOST + Urls.RESET_PASSWORD, requestBean,
                    new HttpResponseHandler() {
                        @Override
                        public void onInnovationSuccess(JsonElement value) {
                            super.onInnovationSuccess(value);
                            SpUtil.setPassword("");
                            Toast.makeText(ResetPasswordActivity.this, "修改密码成功，请您重新登录", Toast.LENGTH_LONG).show();
                            ActivityCache.getScreenManager().popAllActivityExceptOne(LoginActivity.class);
                            startActivity(new Intent(ResetPasswordActivity.this, LoginActivity.class));
                        }

                    }, new HttpClient.NetworkUnavailableListener() {
                        @Override
                        public void networkUnavailable() {
                            Toast.makeText(ResetPasswordActivity.this, "网络连接异常，请检查您的网络", Toast.LENGTH_LONG).show();
                        }
                    });

        } else {
            Toast.makeText(ResetPasswordActivity.this, "新密码与确认密码不一致", Toast.LENGTH_LONG).show();
        }

    }
}
